package com.zyj.exam_app.context;

import com.zyj.exam_app.login.bean.LoginInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
 *  用户信息的上下文，目前主要保存session、用户、菜单信息
 */
public class UserContext {

    private static final String CURRENT_USER_IN_SESSION = "logininfo";
    /**
     * 得到session
     */
    private static HttpSession getSession(){
        //SpringMVC获取session的方式通过RequestContextHolder
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
    }
    /**
     * 设置当前用户到session中
     */
    public static void putCurrebtUser(LoginInfo currentUser) {
        getSession().setAttribute(CURRENT_USER_IN_SESSION,currentUser);
    }
    /**
     * 获取当前用户
     */
    public static LoginInfo getCurreentUser() {
        return (LoginInfo) getSession().getAttribute(CURRENT_USER_IN_SESSION);
    }

    /**
     * 清除当前用户信息
     */
    public static void removeCurrentUser(){
        getSession().removeAttribute(CURRENT_USER_IN_SESSION);
    }
}
